package com.gu.qqdemo.dao;

import com.gu.qqdemo.Bean.FeatureBean;
import com.gu.qqdemo.Bean.FriendBean;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;

public class FeatureDao {

    public List<FeatureBean> findAllShow() {
        Realm realm = Realm.getDefaultInstance();
        RealmResults<FeatureBean> all = realm.where(FeatureBean.class).equalTo("showType", "1").findAll();
        // 关闭beans与数据库的关联（从数据库中复制出数据）
        List<FeatureBean> beans = realm.copyFromRealm(all);
        realm.close();
        return beans;
    }

    public List<FeatureBean> findAllClose() {
        Realm realm = Realm.getDefaultInstance();
        RealmResults<FeatureBean> allclose = realm.where(FeatureBean.class).equalTo("showType", "0").findAll();
        List<FeatureBean> beans = realm.copyFromRealm(allclose);
        realm.close();
        return beans;
    }

    public List<FeatureBean> findAllOpen() {
        Realm realm = Realm.getDefaultInstance();
        RealmResults<FeatureBean> allopen = realm.where(FeatureBean.class).equalTo("showType", "1").findAll();
        List<FeatureBean> beans = realm.copyFromRealm(allopen);
        realm.close();
        return beans;
    }

    public void update(FeatureBean bean) {
        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        //修改
        realm.insertOrUpdate(bean);
        realm.commitTransaction();
        realm.close();
    }

    public void insert(List<FriendBean> friend) {
        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        //修改
        realm.insertOrUpdate(friend);
        realm.commitTransaction();
        realm.close();
    }

    public List<FriendBean> show() {
        Realm realm = Realm.getDefaultInstance();
        RealmResults<FriendBean> query = realm.where(FriendBean.class).findAll();
        // 关闭beans与数据库的关联（从数据库中复制出数据）
        List<FriendBean> beans =realm.copyFromRealm(query);
        realm.close();
        return beans;
    }
}
